class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  add_flash_types :info, :warning, :danger, :success

  before_action :authenticate_user!
  layout :layout_by_resource


  # Build Search Params
  def build_search(search_params)
    sql = ""
    if search_params.present?
      search_params.each do |p|

        if sql.blank?
          if p[1].present?
            sql = sql + "\""+p[0]+"\"='"+p[1]+"'"
          end
        else
          if p[1].present?
            sql = sql + " AND \""+p[0]+"\"='"+p[1]+"'"
          end
        end
      end
    end

    return sql
  end

  private

  def layout_by_resource
    if devise_controller?
      if resource_name == :user &&  action_name == 'new'
        "sign_in"
      else
        "registration"
      end
    else
      "application"
    end
  end



end
